package com.cy.pj.sys.service.impl;

import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;
    @Override
    public PageObject<SysLog> findPageObjects(String username,
                                              Integer pageCurrent) {
        //1.参数校验
        //2.查询当前页记录
        int pageSize=5;
        Page<SysLog> page= PageHelper.startPage(pageCurrent,pageSize);
        List<SysLog> records= sysLogDao.findPageObjects(username);
        //3.封装查询结果
        return new PageObject<>((int)page.getTotal(),records,page.getPages(),pageSize,pageCurrent);
    }
    //写日志放在独立的事务中(Propagation.REQUIRES_NEW)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Async //此注解描述的方法为一个异步切入点方法,此方法会运行在异步池中的线程上.
    @Override
    public void saveObject(SysLog entity) {
        String tName=Thread.currentThread().getName();
        System.out.println("SysLogServiceImpl.saveObject.threadName->"+tName);
        try{ Thread.sleep(5000);}catch (Exception e){}//模拟耗时操作
        sysLogDao.insertObject(entity);
    }
}